<template>
  <el-row class="warp">
    <el-col :span="24" class="warp-breadcrum">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }"><b>首页</b></el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>

    <el-col :span="24" class="warp-main" v-loading="loading" element-loading-text="拼命加载中">
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
          <el-form :inline="true" :model="filters">
            <el-form-item>
              <el-input v-model="filters.name" placeholder="姓名" style="min-width: 240px;" @keyup.enter.native="handleSearch"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleSearch">查询</el-button>
            </el-form-item>
          </el-form>
        </el-col>

      <!--列表-->
      <el-table :data="users" highlight-current-row v-loading="loading" style="width: 100%;">
        <el-table-column type="index" width="120" align="center">
        </el-table-column>
        <el-table-column prop="username" label="姓名" width="240" align="center" sortable>
        </el-table-column>
        <el-table-column prop="nickname" label="昵称" width="240" align="center" sortable>
        </el-table-column>
        <el-table-column prop="sex" label="性别" width="200" align="center" sortable>
        </el-table-column>
        <el-table-column prop="birthday" label="生日" :formatter="formatterDate" min-width="200" align="center" sortable>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="small" @click="showDetailDialog(scope.$index,scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="8" :total="total"
                     style="float:right;">
      </el-pagination>

      <el-dialog title="查看" :visible.sync ="detailsFormVisible" :close-on-click-modal="false">
        <el-form :model="detailsForm" label-width="100px" ref="detailsForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="detailsForm.username" auto-complete="off" disabled></el-input>
          </el-form-item>
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="detailsForm.nickname" auto-complete="off" disabled></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-input v-model="detailsForm.sex" auto-complete="off" disabled></el-input>
          </el-form-item>

          <el-form-item label="年龄">
            <el-input v-model="detailsForm.age" auto-complete="off" disabled></el-input>
          </el-form-item>
          <!-- <el-form-item label="生日" prop="birthday">
            <el-input v-model="detailsForm.birthday" auto-complete="off" disabled></el-input>
          </el-form-item> -->
          <el-form-item label="电话" prop="phone">
            <el-input type="input" v-model="detailsForm.phone" disabled></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="detailsFormVisible = false">取消</el-button>
          <!-- <el-button type="primary" @click.native="editSubmit">提交</el-button> -->
        </div>
      </el-dialog>

    </el-col>
  </el-row>
</template>

<script>
  import util from '../../common/util'
  import API from '../../api/api_user';

  export default {
    data() {
      return {
        filters: {
          name: ''
        },
        loading: false,
        users: [],
        total: 0,
        page: 1,
        limit: 8,


        //查看相关数据
        detailsFormVisible: false,
        detailsForm: {
          username: '',
          nickname: '',
          sex: '',
          age: '',
          birthday: '',
        },

      }
    },
    methods: {
      //性别显示转换
      // formatSex: function (row, column) {
      //   return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
      // },

      formatterDate : function(row,column)
      {
        return util.formatDate.format(new Date(row.birthday),'yyyy-MM-dd');
      },

      handleCurrentChange(val) {
        this.page = val;
        this.search();
      },
      handleSearch(){
        this.total = 0;
        this.page = 1;
        this.search();
      },
      //获取用户列表
      search: function () {
        let that = this;
        let params = {
          page: that.page,
          limit: 8,
          name: that.filters.name
        };

        that.loading = true;
        API.findList(params).then(function (result) {
          that.loading = false;
          console.log(result);
          if (result.code === 200) {
            that.total = result.total;
            that.users = result.data;
          }
        }, function (err) {
          that.loading = false;
          that.$message.error({showClose: true, message: err.toString(), duration: 2000});
        }).catch(function (error) {
          that.loading = false;
          console.log(error);
          that.$message.error({showClose: true, message: '请求出现异常', duration: 2000});
        });
      },
      showDetailDialog: function (index,row)
      {
        let that = this;
        this.detailsFormVisible = true;
        this.detailsForm = Object.assign({},row);
      },
    },
    //显示查看页面

    mounted() {
      this.handleSearch()
    }
  }
</script>

<style scoped>

</style>
